//
//  AIHomeController.m
//  aixianfeng
//
//  Created by 李江波 on 2016/12/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIHomeController.h"

#import "AIHomeModel.h"
#import "AIHomeActivitiesModel.h"
#import "AIHomeIconsModel.h"
#import "AIHomeFocusModel.h"
#import "AIHomeItemModel.h"

//导入图片轮播器的View
#import "AIWheelPlayerView.h"

//导入中间View
#import "AIHomeIconsFourView.h"

//创建活动的View
#import "AIHomeActivitiesView.h"

#import "AIHomeCollectionViewLayout.h"

//导入课重用的头部View
#import "AIHomeCollectionReusableView.h"

//导入itemView 的头文件
#import "AIHomeCollectionViewItemCell.h"

static NSString *FirstCell = @"FirstCell";
static NSString *KindHeadCollectionCell = @"KindHeadCollectionCell";
static NSString *KindHeadID = @"KindHeadID";
static NSString *KindFooterCollectionCell = @"KindFooterCollectionCell";
static NSString *KindFooterID = @"KindFooterID";

@interface AIHomeController ()<UICollectionViewDelegate,UICollectionViewDataSource>

//这里是四种结构的模型数据
@property (nonatomic,strong)NSArray<AIHomeItemModel *> *itemArray;
@property (nonatomic,strong)NSArray<AIHomeFocusModel *> *focusArray;
@property (nonatomic,strong)NSArray<AIHomeActivitiesModel *> *activitiesArray;
@property (nonatomic,strong)NSArray<AIHomeIconsModel *> *iconsArray;

@property (nonatomic,weak)AIWheelPlayerView *wheelPlayerView;

@property (nonatomic,weak)UICollectionView *collectionView;

@property (nonatomic,weak)AIHomeIconsFourView *iconsView;

@property (nonatomic,weak)AIHomeActivitiesView *acitvitesView;

@end

@implementation AIHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取数据
    [self loadData];
    //设置UI界面
    [self setupUI];
    
}

- (void)loadData {
    [H getDataFinshed:^(NSMutableArray<AIHomeFocusModel *> *focusModel, NSMutableArray<AIHomeIconsModel *> *iconsModel, NSMutableArray<AIHomeActivitiesModel *> *activitiesModel, NSError *error) {
        
        if (error) {
            NSLog(@"%@",error);
            return ;
        }
        
        //来到这里证明数据请求成功
        self.focusArray = focusModel;
        self.iconsArray = iconsModel;
        self.activitiesArray = activitiesModel;
        
        //这里得到数据后,我们需要刷新轮播器的数据
        self.wheelPlayerView.focusModelArray = self.focusArray;
        self.iconsView.iconsModelArray = self.iconsArray;
        self.acitvitesView.activitiesArray = self.activitiesArray;
        
        //刷新collectionView
        [self.collectionView reloadData];
    }];
    
    [H getDataItemFinshed:^(NSMutableArray<AIHomeItemModel *> *itemModel, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            return ;
        }
        //来到这这里证明数据请求成功
        self.itemArray = itemModel;
        //刷新collectionView
        [self.collectionView reloadData];
        
    }];

}

- (void)setupUI{
   
    AIHomeCollectionViewLayout *layout = [AIHomeCollectionViewLayout new];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collectionView;
    [self.view addSubview:collectionView];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"AIHomeCollectionViewItemCell" bundle:nil] forCellWithReuseIdentifier:@"ItemNib"];
   // [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:FirstCell];
    
    [self.collectionView registerClass:[AIHomeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KindHeadID];
    
    //约束
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self.view);
        make.leading.mas_equalTo(self.view.mas_leading);
        make.trailing.mas_equalTo(self.view.mas_trailing);
    }];
    
}


#pragma mark
#pragma mark -代理和数据源方法 dataSource,dataDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return  1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.itemArray.count;
}

//这里表示有多少个
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //现在我需要取出模型数据
    AIHomeItemModel *itemModel = self.itemArray[indexPath.row];
    // 1. 取出cell
    AIHomeCollectionViewItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ItemNib" forIndexPath:indexPath];
    cell.itemModel = itemModel;
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        //现在我们就需要出队一个头部视图
        AIHomeCollectionReusableView *reusableCell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KindHeadID forIndexPath:indexPath];
        reusableCell.focusArray = self.focusArray;
        reusableCell.iconsArray = self.iconsArray;
        reusableCell.activitiesArray = self.activitiesArray;
        
        reusableCell.backgroundColor = [UIColor redColor];
        return reusableCell;
    }
    
    return nil;
}



@end
